//
//  REBaseViewController.swift
//  GoGlobalis
//
//  Created by goglobails on 2018/10/8.
//  Copyright © 2018年 Webkul. All rights reserved.
//

import UIKit

open class REBaseViewController: UIViewController {

    override open func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = REViewBackgroundColor()
        self.edgesForExtendedLayout = .bottom
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.automaticallyAdjustsScrollViewInsets = false
        self.initialBackNaviItem()
    }
    
    public var baseConfigure: REBaseConfigure? {
        didSet {
            if baseConfigure != nil {
                self.navigationController?.navigationBar.barTintColor = baseConfigure!.navigationBar_barTintColor
                self.navigationController?.navigationBar.tintColor = baseConfigure!.navigationBar_tintColor
                self.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: ""), style: .done, target: nil, action: nil)
                self.navigationController?.navigationBar.barStyle = baseConfigure!.navigationBar_barStyle;
                self.navigationController?.navigationBar.titleTextAttributes = baseConfigure?.navigationBar_titleAttributes
                self.view.backgroundColor = baseConfigure?.viewBackgroundColor
                
                let appearance: UINavigationBar = UINavigationBar.appearance()
                appearance.isTranslucent = false
                UIApplication.shared.statusBarStyle = baseConfigure!.statusBarStyle
            } else {
                self.navigationController?.navigationBar.barTintColor = UIColor.white
                self.navigationController?.navigationBar.tintColor = REHexRGB(0x003680)
                self.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: ""), style: .done, target: nil, action: nil)
                self.navigationController?.navigationBar.barStyle = UIBarStyle.default;
                self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: REFontRegular(18.0), NSAttributedStringKey.foregroundColor:UIColor.white]
                self.view.backgroundColor = UIColor.white
                
                let appearance: UINavigationBar = UINavigationBar.appearance()
                appearance.isTranslucent = false
                UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
            }
        }
    }
    
    public lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.frame = CGRect(x: 0, y: 0, width: REScreenWidth, height: 0)
        scrollView.backgroundColor = UIColor.white
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        self.view.addSubview(scrollView)
        
        var edgeInsets = UIEdgeInsets.zero
        if #available(iOS 11.0, *) {
            edgeInsets.top = (UIApplication.shared.keyWindow?.safeAreaInsets.top)!
            edgeInsets.left = (UIApplication.shared.keyWindow?.safeAreaInsets.left)!
            edgeInsets.right = (UIApplication.shared.keyWindow?.safeAreaInsets.right)!
            edgeInsets.bottom = (UIApplication.shared.keyWindow?.safeAreaInsets.bottom)!
        }
        scrollView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-edgeInsets.bottom)
            make.centerY.equalToSuperview()
        }
        return scrollView
    }()
    
    private func initialBackNaviItem() {
        
        if self.navigationController == nil {
            return
        }
        
        if self.navigationController!.viewControllers.count <= 1 {
            return
        }
        
        let backItem = UIBarButtonItem(image: UIImage(named: "gg_common_back"), style: .plain, target: self, action: #selector(backNaviItemClick(_:)))
        self.navigationItem.leftBarButtonItem = backItem
    }
    
    @objc func backNaviItemClick(_ sender: UIBarButtonItem) {
        if self.allowBackToPrevious() {
            self.backToPrevious()
        }
    }
    
    public func allowBackToPrevious() -> Bool {
        return true
    }
    
    public func backToPrevious() {
        self.pop()
    }

    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
